#/bin/bash


# 检查是否有参数传递给脚本
if [ $# -eq 0 ]; then
    echo "至少提供一个tar文件名."
    exit 1
fi


F=$1
tar -xf $F "${@:2}"  2> /dev/null &


TAR_PID=$!

TOTAL_SIZE=$(stat -c %s "$F" 2>/dev/null)


if [ $TOTAL_SIZE -eq 0 ]; then
   TOTAL_SIZE=1
fi

while kill -0 $TAR_PID 2> /dev/null; do
    CURRENT_SIZE=$(lsof -o0 -oFo $F 2>/dev/null | awk -Ft '/^o/{print $2}' | sort -u | tail -n 1)

    if [ -z "$CURRENT_SIZE" ]; then
       CURRENT_SIZE=0
    fi
    PERCENTAGE=$(awk -v cur=$CURRENT_SIZE -v total=$TOTAL_SIZE 'BEGIN {printf "%d", (cur/total)*100}')
    echo -ne "解压进度  $PERCENTAGE%  \r"
    sleep 1
done
